package com.lkm.app_ui.m.Imple.hsj522;

import com.lkm.app_ui.log.SystemLogger;
import com.lkm.frame.task.ProgressDataImpl;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.AbsConnectModule;
import com.lkm.hardware_connect.HardwareControlerHelp;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.NumberHelp;
import com.lkm.serialport_com.connect.SerialportConnectModule;

import java.io.IOException;


public class ReadTask extends TaskAdapter<IHardwareControler, ReadResponEntity, Void> {
    public static final String TAG = "SReadTask";


    private final int BUFSIZE = 512;
    private byte[] buf = new byte[BUFSIZE];

    public static final int ProgressData_data = 1;

    private AbsConnectModule cardCircuitCom;

    public ReadTask(AbsConnectModule cardCircuitCom, ITaskExecutor<IHardwareControler, ReadResponEntity, Void> taskExecutor, TaskManager mTaskManager) {
        super(taskExecutor, ReadTask.class.getName(), mTaskManager);
        this.cardCircuitCom = cardCircuitCom;

    }


    @Override
    public void doExecuting(IHardwareControler param) {
        super.doExecuting(param);
        mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
            @Override
            public void run() {
                try {
                    doAsyncExecuting(param);
                } catch (Throwable t) {
                    cancel();
                    t.printStackTrace();
                }
            }
        });

    }

    private void doAsyncExecuting(IHardwareControler hardwareControler) {

        SystemLogger.HSJ522.onReadStart();
        while (getIsContinue()) {
            try {




                byte[] checkData = checkData(hardwareControler);

                byte SEQNR = checkData[1];

                byte[] status = HardwareControlerHelp.readFull(hardwareControler, 1);
                byte stute = status[0];

                byte[] length = HardwareControlerHelp.readFull(hardwareControler, 1);

                int contentLength = length[0];

                byte[] data = null;
                if (contentLength == 0) {
                    data = new byte[0];
                } else {

                    data = HardwareControlerHelp.readFull(hardwareControler, contentLength);
                }

                byte[] CRC8 = HardwareControlerHelp.readFull(hardwareControler, 1);
                byte[] endFLAG = HardwareControlerHelp.readFull(hardwareControler, 1);


                byte calcCRC8 = HSJ800Help.crc(SEQNR, stute,  data);



                if (calcCRC8 == CRC8[0] && endFLAG[0] == (byte) 0x03) {
                    SystemLogger.HSJ522.onReadOK(status);
                    SystemLogger.HSJ522.onReadOK(data);
                    progressUpdate(new ProgressDataImpl(0, ProgressData_data, new ReadResponEntity(SEQNR, stute, data)));

                } else {
                    SystemLogger.HSJ522.onReadInvalid(data);
                }

            } catch (IOException e) {
                cardCircuitCom.disConnectForError("");
            }

        }

    }




    private byte[] checkData(IHardwareControler hardwareControler) throws IOException {


        byte last = 0;
        byte[] tempFrData;
        int readHeadCount = 0;
        for (; ; ) {
            tempFrData = HardwareControlerHelp.readFull(hardwareControler, 1);


            if (last == (byte) 0x20) {

                if (tempFrData[0] == (byte) 0x7E || true) {

                    break;
                }
            } else {

                readHeadCount++;


                if (SystemLogger.HSJ522.isDebug && readHeadCount > 1) {
                    String logs = NumberHelp.bytesToHex(last) + "  " + NumberHelp.bytesToHex(tempFrData[0]);
                    SystemLogger.HSJ522.onReadBad(logs);
                }

                last = tempFrData[0];
            }

        }

        return new byte[]{last, tempFrData[0]};
    }


}
